package com.ss.android.article.base.feature.feed.presenter;

import android.content.Context;
import android.webkit.WebView;

import com.bytedance.article.common.c.TTUtils;
import com.bytedance.common.utility.NetworkType;
import com.bytedance.common.utility.Logger;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.feature.app.b.DBHelper;
import com.ss.android.article.base.feature.model.Article;
import com.ss.android.common.app.LifeCycleMonitor;
import com.ss.android.common.applog.AppLog;
import com.ss.android.common.load.AsyncLoader;
import com.ss.android.common.util.NetworkStatusMonitor;
import com.ss.android.http.legacy.a.LegacyHeader;
import com.ss.android.newmedia.util.AppUtil;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

public final class WebArticlePreloadHelper implements LifeCycleMonitor {
    class PreloadQueue extends LinkedHashMap {
        final int mMaxSize;
        private static final long serialVersionUID = -586159710183835312L;

        public PreloadQueue(int maxSize, int initialCapacity) {
            super(initialCapacity, 0.75f, true);
            this.mMaxSize = maxSize;
            if (maxSize <= 0) {
                throw new IllegalArgumentException("maxSize <= 0");
            }
        }

        protected boolean removeEldestEntry(Map.Entry entry) {
            return this.size() > this.mMaxSize;
        }
    }

    PreloadQueue mPreloadQueue;
    boolean b;
    Article mArticle;
    NetworkStatusMonitor mNetworkMonitor;
    Context mContext;
    private boolean f;
    private boolean g;
    private AppData mAppData;
    private String i;
    private String j;
    private AsyncLoader.LoaderProxy k;
    private AsyncLoader l;

    public WebArticlePreloadHelper(Context context, NetworkStatusMonitor arg7) {
        this.f = false;
        this.g = false;
        this.mPreloadQueue = new PreloadQueue(10, 16);
        this.b = false;
        this.mArticle = null;
        this.k = new AsyncLoader.LoaderProxy(){
            public final Object doInBackground(Object key, Object param, Object extra) {
                return Integer.valueOf(WebArticlePreloadHelper.this.a(((Article) param), ((String) key)));
            }

            public final void onLoaded(Object key, Object param, Object extra, Object result) {
                int v0 = result != null ? ((Integer) result).intValue() : 0;
                Article article = WebArticlePreloadHelper.this.mArticle;
                WebArticlePreloadHelper.this.mArticle = null;
                WebArticlePreloadHelper.this.b = false;
                if(article != null) {
                    WebArticlePreloadHelper.this.mPreloadQueue.remove(Long.valueOf(article.groupId));
                }

                if(article != null && key != null) {
                    if(Logger.debug()) {
                        Logger.v("WebArticlePreloadHelper", "preload web done " + article.groupId + " " + v0 + " " + key);
                    }

                    if(v0 != 0 && v0 != 200) {
                    }else {
                        long v6 = System.currentTimeMillis();
                        article.webTcLoadTime = v6;
                        DBHelper.getInstance(WebArticlePreloadHelper.this.mContext).saveWebLoadTime(article.groupId, article.itemId, v6, true);
                    }
                }
                WebArticlePreloadHelper.this.preload();
            }
        };
        this.l = new AsyncLoader(8, 1, this.k);
        this.mContext = context.getApplicationContext();
        this.mNetworkMonitor = arg7;
        this.mAppData = AppData.getInstance();
        this.i = this.mAppData.a(this.mContext, null);
        this.j = AppUtil.getUserAgent(this.mContext, (WebView) null);
    }

    final int a(Article article, String arg6) {
        int v0_2;
        try {
            ArrayList v1 = new ArrayList();
            v1.add(new LegacyHeader("Accept-Encoding", "gzip"));
            String userAgent = this.i;
            if ((article.hasArticleUrl()) && (article.e())) {
                userAgent = this.j;
            }

            AppUtil.appendHeaders(v1, userAgent, article.wapHeaders);
            v0_2 = AppUtil.doHttpRequest(arg6, v1).responseCode;
        } catch (Exception v0) {
            v0_2 = AppLog.w();
        }

        return v0_2;
    }

    final void preload() {
        String articleUrl;
        Article article;
        Object v10 = null;
        if (!this.f && (this.g) && !this.b && !this.mPreloadQueue.isEmpty()) {
            NetworkType v1 = this.mNetworkMonitor.mCurrrentNetworkType;
            if (v1 != null && v1 != NetworkType.NONE) {
                long v2 = System.currentTimeMillis();
                Iterator<Map.Entry> v4 = this.mPreloadQueue.entrySet().iterator();
                do {
                    if (v4.hasNext()) {
                        article = (Article) v4.next().getValue();
                        v4.remove();
                        if (!article.shouldPreload(v1)) {
                            continue;
                        }

                        if (v2 - article.N < 30000) {
                            continue;
                        }

                        articleUrl = ((Article) article).articleUrl;
                        if (!TTUtils.isValidUrl(articleUrl)) {
                            continue;
                        }

                        break;
                    }

                    return;
                }
                while (true);

                ((Article) article).N = v2;
                this.mArticle = ((Article) article);
                this.b = true;
                if (Logger.debug()) {
                    Logger.v("WebArticlePreloadHelper", "preload web type: " + ((Article) article).articleUrl);
                }

                this.l.addTask(articleUrl, article, v10, v10);
            }
        }
    }

    public final void onDestroy() {
        this.f = true;
        this.g = false;
        this.l.stop();
    }

    public final void onResume() {
        this.g = true;
        this.l.resume();
        this.preload();
    }

    public final void onPause() {
        this.g = false;
    }

    public final void onStop() {
        this.g = false;
        this.l.pause();
    }
}

